package is.progetto2.activity;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ResultSchedule {

	private Map<Job, Integer> map;
	private int duration;

	protected ResultSchedule(Map<Job, Integer> map) {
		this.map = map;
		findDuration();
	}

	private void findDuration() {
		int duration = 0;
		Set<Job> set = map.keySet();
		for (Job j : set) {
			if (map.get(j)+j.getDuration() >= duration) {
				int max = map.get(j);
				duration = max + j.getDuration() - 1;
			}
		}
		this.duration = duration;
	}
	
	public int getJobSize(){
		return this.map.keySet().size();
	}

	public boolean isJobTime(Job j, int time) {
		int tmp = map.get(j);
		if (tmp == time) {
			return true;
		}
		if (time - tmp < j.getDuration() && time - tmp >= 0) {
			return true;
		}
		return false;
	}

	public Job getJob(int index) {
		Iterator<Job> it = map.keySet().iterator();
		int i = -1;
		Job j = null;
		while (it.hasNext()) {
			j = it.next();
			i++;
			if (i == index) {
				return j;
			}
		}
		throw new IllegalArgumentException();
	}

	public int getDuration() {
		return duration;
	}

	@Override
	public String toString() {
		return "ResultSchedule [map=" + map + ", duration=" + duration + "]";
	}
}
